package com.goods.business.service.imp;

import com.goods.business.converter.DateConvert;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    //物资类别列表
    @Override
    public List<ProductCategoryTreeNodeVO> categoryTree(String level) {//建集合 查 遍历 建对象
        //真正返回的VO集合
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOOneList = new ArrayList<>();
        //先查询一级分类 一级分类的pid为0
        List<ProductCategory> productCategoryOne = productCategoryMapper.categoryTree(0L);
        //循环遍历一级分类
        for (ProductCategory categoryOne : productCategoryOne) {
            ProductCategoryTreeNodeVO categoryTreeNodeVOOne = new ProductCategoryTreeNodeVO();
            //二级分类集合
            List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOTwoList = new ArrayList<>();
            //通过一级分类的id 查询 子二级分类
            List<ProductCategory> productCategoryTwo = productCategoryMapper.categoryTree(categoryOne.getId());
            //循环遍历二级分类
            for (ProductCategory categoryTwo : productCategoryTwo) {
                ProductCategoryTreeNodeVO categoryTreeNodeVOTwo = new ProductCategoryTreeNodeVO();
                List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOThreeList = null;//为了二级分类Children为null
                if ("一二三".equals(level)) {
                    productCategoryTreeNodeVOThreeList = new ArrayList<>();
                    //三级分类集合
                    //通过二级分类的id 查询 子三级分类
                    List<ProductCategory> productCategoryThree = productCategoryMapper.categoryTree(categoryTwo.getId());
                    //循环遍历三级分类
                    for (ProductCategory categoryThree : productCategoryThree) {
                        ProductCategoryTreeNodeVO categoryTreeNodeVOThree = new ProductCategoryTreeNodeVO();
                        BeanUtils.copyProperties(categoryThree, categoryTreeNodeVOThree);
                        categoryTreeNodeVOThree.setLev(3);
                        categoryTreeNodeVOThree.setChildren(null);
                        productCategoryTreeNodeVOThreeList.add(categoryTreeNodeVOThree);
                    }
                }
                BeanUtils.copyProperties(categoryTwo, categoryTreeNodeVOTwo);
                categoryTreeNodeVOTwo.setLev(2);
                categoryTreeNodeVOTwo.setChildren(productCategoryTreeNodeVOThreeList);
                productCategoryTreeNodeVOTwoList.add(categoryTreeNodeVOTwo);
            }
            BeanUtils.copyProperties(categoryOne, categoryTreeNodeVOOne);
            categoryTreeNodeVOOne.setLev(1);
            categoryTreeNodeVOOne.setChildren(productCategoryTreeNodeVOTwoList);
            productCategoryTreeNodeVOOneList.add(categoryTreeNodeVOOne);
        }
        return productCategoryTreeNodeVOOneList;
    }

    //添加分类
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = new ProductCategory();
        BeanUtils.copyProperties(productCategoryVO, productCategory);
        productCategory.setCreateTime(new Date());
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.insert(productCategory);
    }

    //修改分类时回显信息
    @Override
    public ProductCategory selectProductCategoryById(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    //修改分类信息
    @Override
    public void update(Long id, Map editRuleForm) {
        ProductCategory productCategory = new ProductCategory();
        //网上搜的转换器 自己又做了封装
        DateConvert.Convert();
        try {
            org.apache.commons.beanutils.BeanUtils.populate(productCategory, editRuleForm);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }

    @Override
    public boolean delete(Long id) {
        //删除分类  有子分类的、和物资引用的分类不能删除
        //先查询
        List<ProductCategory> productCategoryList = productCategoryMapper.categoryTree(id);
        if (CollectionUtils.isEmpty(productCategoryList)) {
            //无子分类
            //TODO 判断是否有引用
            productCategoryMapper.deleteByPrimaryKey(id);
            return true;
        } else {
            //有子分类 不能删
            return false;
        }
    }
}
